﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace TheUglyGame
{
    class MenuItem
    {
        private Vector2         mPos,
                                mOrigin;
        private int             mWidth,
                                mHeight;
        private Const.GameState mGamestate;
        private Texture2D[]     mTexSet;
        private Texture2D       mCurrentTex;
        private Rectangle       mRect;
        private Color           mCol;

        public  Const.GameState Gamestate   { get { return mGamestate; }  set { mGamestate = value; } }
        public  Texture2D[]     TexSet      { get { return mTexSet; } }
        public  Texture2D       CurrentTex  { get { return mCurrentTex; } set { mCurrentTex = value; } }
        public  Rectangle       Rect        { get { return mRect; } }
        public  Color           Col         { get { return mCol; }        set { mCol = value; } }

        private void SetAttributes(float PosX, float PosY, int Width, int Height, Texture2D[] TexSet)
        {
            mPos = new Vector2(PosX, PosY);
            mWidth = Width;
            mHeight = Height;
            mOrigin = new Vector2(mWidth / 2, mHeight / 2);
            mTexSet = TexSet;
            mCurrentTex = TexSet[0];
            mRect = new Rectangle((int)mPos.X, (int)mPos.Y, mWidth, Height);
        }

        public MenuItem(float PosX, float PosY, int Width, int Height, Texture2D[] TexSet)
        {
            mTexSet = new Texture2D[2];
            SetAttributes(PosX, PosY, Width, Height, TexSet);
        }
    }

    abstract class MenuBase
    {
        private GraphicsDevice    mGraphicsDevice;
        private SpriteBatch       mSpritebatch;
        private List<Texture2D>   mTexSetBackground;
        private SpriteFont        mFont;
        private Vector2           mPos,
                                  mButtonPos,
                                  mOrigin;
        private int               mWidth,
                                  mHeight,
                                  mButtonCount;
        private string            mHeadline;
        private Rectangle         mRectBackground;
        private bool              mClassicMenu;           //klassische Menü mit Buttons oder ohne Buttons z.B. Pause mit Stats
        private List<MenuItem>    mButtons;
        private string            mAppPath;

        protected SpriteBatch     Spritebatch       { get { return mSpritebatch; } }
        protected int             ButtonCount       { get { return mButtonCount; } }
        protected Vector2         ButtonPos         { get { return mButtonPos; } }
        protected string          AppPath           { get { return mAppPath; } }
        protected List<MenuItem>  Buttons           { get { return mButtons; }          set { mButtons = value; } }
        protected List<Texture2D> TexSetBackground  { get { return mTexSetBackground; } set { mTexSetBackground = value; } }

        public void SetSpritebatch(SpriteBatch Spritebatch)
        {
            mSpritebatch = Spritebatch;
        }

        private void SetAttributes(string Headline, float MenuDrawPosX, float MenuDrawPosY, int MenuWidth, int MenuHeight)
        {
            mHeadline = Headline;
            mPos = new Vector2(MenuDrawPosX, MenuDrawPosY);
            mWidth = MenuWidth;
            mHeight = MenuHeight;
            mRectBackground = new Rectangle((int)mPos.X, (int)mPos.Y, mWidth, mHeight);
            mOrigin = new Vector2((int)mWidth / 2, (int)mHeight / 2);

            DirectoryInfo di = new DirectoryInfo(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location));
            mAppPath = di.FullName;
        }

        public MenuBase(string Headline, float MenuDrawPosX, float MenuDrawPosY, int MenuWidth, int MenuHeight, int ButtonCount)
        {
            SetAttributes(Headline, MenuDrawPosX, MenuDrawPosY, MenuWidth, MenuHeight);
            mButtonCount = ButtonCount;
            mButtonPos.X = mOrigin.X - (Const.MenuButtonWidth / 2);
            mButtonPos.Y = mOrigin.Y - (((Const.MenuButtonHeight * ButtonCount) + (Const.MenuButtonSpace * (ButtonCount - 1))) / 2) + Const.MenuButtonHeight;
            mButtons = new List<MenuItem>();
            mClassicMenu = true;
        }

        public MenuBase(string Headline, SpriteFont Font, float MenuDrawPosX, float MenuDrawPosY, int MenuWidth, int MenuHeight)
        {
            SetAttributes(Headline, MenuDrawPosX, MenuDrawPosY, MenuWidth, MenuHeight);
            mFont = Font;
            mClassicMenu = true;
        }

        protected void SetGraphicsDeviceAndSpritebatch(SpriteBatch Spritebatch)
        {
            mSpritebatch = Spritebatch;
        }

        public abstract void Load(ContentManager Content, SpriteBatch Spritebatch);
        public abstract void Unload();
        public abstract void Draw();
        public abstract Const.GameState Update(MouseState Mousestate);
    }
}
